﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SharpSoft.WPF.Controls
{
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;
    using System.Windows.Media.Animation;

    /// <summary>
    /// 有图片切换效果的图形控件
    /// </summary>
    public class ImageReplace : Grid
    {
        static ImageReplace()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ImageReplace), new FrameworkPropertyMetadata(typeof(ImageReplace)));
        }
        Image img1, img2;
        public ImageReplace()
        {
            img1 = new Image();
            img2 = new Image();
            this.Children.Add(img2);
            this.Children.Add(img1);


            this.ClipToBounds = true;
        }

        public static readonly RoutedEvent ImageFailedEvent;

        public ImageSource Source
        {
            get { return (ImageSource)GetValue(SourceProperty); }
            set { SetValue(SourceProperty, value); }
        }

        public static readonly DependencyProperty SourceProperty =
            DependencyProperty.Register("Source", typeof(ImageSource), typeof(ImageReplace), new PropertyMetadata(null));



        public Stretch Stretch
        {
            get { return (Stretch)GetValue(StretchProperty); }
            set { SetValue(StretchProperty, value); }
        }

        public static readonly DependencyProperty StretchProperty =
            DependencyProperty.Register("Stretch", typeof(Stretch), typeof(ImageReplace), new PropertyMetadata(Stretch.UniformToFill));



        public StretchDirection StretchDirection
        {
            get { return (StretchDirection)GetValue(StretchDirectionProperty); }
            set { SetValue(StretchDirectionProperty, value); }
        }

        public static readonly DependencyProperty StretchDirectionProperty =
            DependencyProperty.Register("StretchDirection", typeof(StretchDirection), typeof(ImageReplace), new PropertyMetadata(StretchDirection.Both));

        public ImageReplaceAnimation AnimationType { get; set; } = ImageReplaceAnimation.TranslationToLeft;

        public Duration AnimationDuration { get; set; } = new Duration(new TimeSpan(0, 0, 1));

        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            base.OnPropertyChanged(e);
            if (e.Property == ImageReplace.StretchProperty)
            {//切换图片·
                img1.Stretch = img2.Stretch = this.Stretch;
            }
            else if (e.Property == ImageReplace.StretchDirectionProperty)
            {
                img1.StretchDirection = img2.StretchDirection = this.StretchDirection;
            }
            else if (e.Property == ImageReplace.SourceProperty)
            {
                img2.Source = this.Source;
                beginAnimation();
            }
        }

        private void beginAnimation()
        {
            var sb = createAni(this.AnimationType);

            sb.Completed += Sb_Completed;
            sb.Begin();
        }

        private void Sb_Completed(object sender, EventArgs e)
        {
            img1.Margin = img2.Margin = new Thickness();
            img1.Source = img2.Source;
        }

        private Storyboard createAni(ImageReplaceAnimation ani)
        {
            Storyboard sb = new Storyboard();
            switch (ani)
            {
                case ImageReplaceAnimation.TranslationToLeft:


                    ThicknessAnimation da1 = new ThicknessAnimation(new Thickness(0), new Thickness(-this.ActualWidth, 0, this.ActualWidth, 0), this.AnimationDuration);
                    ThicknessAnimation da2 = new ThicknessAnimation(new Thickness(this.ActualWidth, 0, -this.ActualWidth, 0), new Thickness(0), this.AnimationDuration);
                    Storyboard.SetTarget(da1, img1);
                    Storyboard.SetTarget(da2, img2);
                    Storyboard.SetTargetProperty(da1, new PropertyPath("Margin"));
                    Storyboard.SetTargetProperty(da2, new PropertyPath("Margin"));
                    sb.Children.Add(da1);
                    sb.Children.Add(da2);
                    break;
                default:
                    break;
            }

            return sb;
        }

        protected override void OnRender(DrawingContext dc)
        {
            base.OnRender(dc);
        }
    }

    public enum ImageReplaceAnimation
    {
        /// <summary>
        /// 向左平移动画
        /// </summary>
        TranslationToLeft
    }
}
